package com.qiche.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.model.Area;
import com.qiche.model.Region;
import com.qiche.service.AreaService;
import com.qiche.service.RegionService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("areaService")
public class AreaServiceImpl extends BaseService<Area> implements AreaService {


	@Override
	public List<Area> getAllProvinces() {
		Example example=new Example(Area.class);
		Example.Criteria criteria=example.createCriteria();
		List<Area> areaList=mapper.selectByExample(example);
		List<Area> provinceList=new ArrayList<>();
		Map<Integer, List<Area>> provinceMap=areaList.stream().collect(Collectors.groupingBy(Area::getProvinceCode));
		for (Map.Entry<Integer,List<Area>> entry:provinceMap.entrySet()
			 ) {
			provinceList.add(entry.getValue().get(0));
		}
		return  provinceList;
	}

	@Override
	public List<Area> getAllCitysByProviceCode(String provinceCode) {
		Example example=new Example(Area.class);
		Example.Criteria criteria=example.createCriteria();
		criteria.andEqualTo("provinceCode",provinceCode);
		List<Area> areaList=mapper.selectByExample(example);
		List<Area> cityList=new ArrayList<>();
		Map<Integer, List<Area>> cityMap=areaList.stream().collect(Collectors.groupingBy(Area::getCityCode));
		for (Map.Entry<Integer,List<Area>> entry:cityMap.entrySet()
				) {
			cityList.add(entry.getValue().get(0));
		}
		return cityList;
	}

	@Override
	public List<Area> getAllDistrictsByCityCode(String cityCode) {
		Example example=new Example(Area.class);
		Example.Criteria criteria=example.createCriteria();
		criteria.andEqualTo("cityCode",cityCode);
		List<Area> areaList=mapper.selectByExample(example);
		List<Area> districtList=new ArrayList<>();
		Map<Integer, List<Area>> districtMap=areaList.stream().collect(Collectors.groupingBy(Area::getDistrictCode));
		for (Map.Entry<Integer,List<Area>> entry:districtMap.entrySet()
				) {
			districtList.add(entry.getValue().get(0));
		}
		return  districtList;
	}

	/*@Override
	public PageInfo<Region> selectByPage(Region merchant, int start, int length) {
		int page = start / length + 1;
		Example example = new Example(Region.class);
		Example.Criteria criteria = example.createCriteria();
		// 分页查询
		PageHelper.startPage(page, length);
		List<Region> regionList = selectByExample(example);
		return new PageInfo<>(regionList);
	}

	@Override
	public Region selectById(Long regionId) {
		Example example = new Example(Region.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", regionId);
		List<Region> regionList = selectByExample(example);
		if (regionList.size() > 0) {
			return regionList.get(0);
		}
		return null;
	}

	@Override
	public List<Region> selectAll() {
		return selectByExample(null);
	}*/

}
